"""
插件系统模块
"""

import pkgutil
import importlib

# 插件模块配置
PLUGINS_VERSION = '1.0.0'
print(f"正在加载插件模块 v{PLUGINS_VERSION}...") 

class PluginManager:
    """插件管理器类"""
    
    def __init__(self):
        self.plugins = {}
        self.load_plugins()
    
    def load_plugins(self):
        """加载所有插件"""
        for importer, modname, ispkg in pkgutil.iter_modules(__path__):
            if modname.startswith('plugin_'):
                try:
                    module = importlib.import_module(f'.{modname}', __name__)
                    if hasattr(module, 'register'):
                        module.register(self)
                        self.plugins[modname] = module
                        print(f"已加载插件: {modname}")
                except ImportError as e:
                    print(f"无法加载插件 {modname}: {e}")
    
    def register_plugin(self, name, plugin_obj):
        """注册插件"""
        self.plugins[name] = plugin_obj
        print(f"已注册插件: {name}")
    
    def get_plugin(self, name):
        """获取插件"""
        return self.plugins.get(name)
    
    def list_plugins(self):
        """列出所有已加载的插件"""
        return list(self.plugins.keys())
    
    def execute_plugin(self, name, method_name, *args, **kwargs):
        """执行插件方法"""
        plugin = self.get_plugin(name)
        if plugin and hasattr(plugin, method_name):
            method = getattr(plugin, method_name)
            if callable(method):
                return method(*args, **kwargs)
        return None

# 全局插件管理器
plugin_manager = PluginManager()

__all__ = ['plugin_manager', 'PluginManager'] 

